﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APPLICATION.Dict.City
{
    public partial class FrmNewCity : Form
    {        
        private int CodeID;
        private int Level;

            

        public FrmNewCity(int PCodeID, int PLevel)
        {
            InitializeComponent();

            DataView DVCountry = new DataView(dataSet1.DtCity);
            DataView DVRegion = new DataView(dataSet1.DtCity);
            DataView DVCity = new DataView(dataSet1.DtCity);
            
            CodeID=PCodeID;
            Level = PLevel;

            bindingSourceCountry.DataSource = DVCountry;
            bindingSourceRegion.DataSource = DVRegion;
            bindingSourceCity.DataSource = DVCity;            
        }

        private void ManageControlsState()
        {            
            switch (Level)
            {
                case 0:
                    panelCity.Visible = false;
                    panelRegion.Visible = false;
                    break;
                case 1:
                    textBoxCountry.Enabled = false;
                    panelCity.Visible = false;
                    break;
                default :
                    textBoxCountry.Enabled = false;
                    textBoxRegion.Enabled = false;
                    break;
            }
        }

        private void FormSize()
        {
            switch (Level)
            {
                case 0:
                    this.Height = panelCountry.Height + panel4.Height +35 ;
                    break;
                case 1:
                    this.Height = panelCountry.Height + panelRegion.Height + panel4.Height + 40 ;
                    break;
                default:
                    this.Height = panelCountry.Height + panelRegion.Height + panelCity.Height + panel4.Height +40 ;
                    break;
            }
            
        }

        private void FilterData()
        {
            char[] Cpliter = new char[1];
            Cpliter[0]='.';
            string[] TreeStr;
            DataRow dr;
            dr=dataSet1.DtCity.Rows.Find(CodeID);
            TreeStr = dr["hrccrt"].ToString().Split(Cpliter, StringSplitOptions.RemoveEmptyEntries);
            switch(Level)
            {
                case 1:
                    this.bindingSourceCountry.Filter = "CodeID=" + TreeStr[0];
                    break;
                case 2:
                    this.bindingSourceCountry.Filter = "CodeID=" + TreeStr[0];                                        
                    this.bindingSourceRegion.Filter = "CodeID=" + TreeStr[1];                    
                    break;
            }            
        }

        private void FrmNewCity_Load(object sender, EventArgs e)
        {
            ManageControlsState();
            FormSize();           

            textBoxCountry.DataBindings.Add("Tag", bindingSourceCountry, "CodeID");
            textBoxRegion.DataBindings.Add("Tag", bindingSourceRegion, "CodeID");


            switch (Level)
            {
                case 0:                    
                    this.Text = "Добавить стран";
                    break;
                case 1:
                    FilterData();
                    textBoxCountry.DataBindings.Add("Text", bindingSourceCountry, "hName");
                    this.Text = "Добавить регион";
                    break;
                case 2:
                    FilterData();
                    textBoxCountry.DataBindings.Add("Text", bindingSourceCountry, "hName");
                    textBoxRegion.DataBindings.Add("Text", bindingSourceRegion, "hName");

                    this.Text = "Добавить город";
                    break;
            }
        }

        private void textBoxCountry_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxCountry.Text))
            {
                errorProvider1.SetError(textBoxCountry, "Plz enter text");
            }
            else
            {
                errorProvider1.SetError(textBoxCountry, "");
            }
        }

        private void textBoxRegion_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxRegion.Text))
            {
                errorProvider1.SetError(textBoxRegion, "Plz enter text");
            }
            else
            {
                errorProvider1.SetError(textBoxRegion, "");
            }
        }

        private void textBoxCity_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxCity.Text))
            {
                errorProvider1.SetError(textBoxCity, "Plz enter text");
            }
            else
            {
                errorProvider1.SetError(textBoxCity, "");
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //If not error
            if (errorProvider1.GetError(textBoxCountry) == "" && errorProvider1.GetError(textBoxRegion) == "" && errorProvider1.GetError(textBoxCity) == "")
            {               
                switch (Level)
                {
                    case 0:
                        if (dataSet1.InsertDataCity(textBoxCountry.Text, 0))
                        {
                            textBoxCountry.ResetText();
                        }
                        break;
                    case 1:
                        if (dataSet1.InsertDataCity(textBoxRegion.Text, Convert.ToInt32((object)textBoxCountry.Tag)))
                        {
                            textBoxRegion.ResetText();
                        }
                        break;
                    default :
                        if(dataSet1.InsertDataCity(textBoxCity.Text, Convert.ToInt32((object) textBoxRegion.Tag)))
                        {
                            textBoxCity.ResetText();
                        }
                        break;
                }                
            }               
        }
    }
}